﻿namespace ManagerClinic.DanhMuc
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using System.Collections;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmQuocGia : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler ButtonClicked;
        int State = 0;
        DmQuocGiaEntity dmQuocGiaEntity = null;
        DmQuocGiaBusiness dmQuocGiaBusiness = new DmQuocGiaBusiness();
     
        public frmCapNhatDmQuocGia(int state)
        {
            InitializeComponent();
            
            this.State = state;
           
        }

        public frmCapNhatDmQuocGia(int state, DmQuocGiaEntity obj)
        {
            InitializeComponent();

            this.State = state;
            dmQuocGiaEntity = obj;

            txtMaQuocGia.Text = dmQuocGiaEntity.MaQuocGia;
            txtTenQuocGia.Text = dmQuocGiaEntity.TenQuocGia;
            txtMoTa.Text = dmQuocGiaEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmQuocGiaEntity.TrangThaiSuDung;

            txtMaQuocGia.Enabled = false;
        }

        void frmCapNhatDmQuocGia_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaQuocGia, "mã quốc gia") == false) return;

            dmQuocGiaEntity = new DmQuocGiaEntity()
            {
                MaQuocGia = txtMaQuocGia.Text,
                TenQuocGia = txtTenQuocGia.Text,
                MoTa = txtMoTa.Text,
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };

            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmQuocGiaBusiness.GetDmQuocGia(txtMaQuocGia.Text) != null)
                {
                    if (Validation.ValidPrimaryKey(txtMaQuocGia, "mã quốc gia") == false) return;
                }

                dmQuocGiaBusiness.InsertDmQuocGia(dmQuocGiaEntity);
                Validation.ClearControl(this);
                txtMaQuocGia.Focus();

            }
            else
            {
                dmQuocGiaBusiness.EditDmQuocGia(dmQuocGiaEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (ButtonClicked != null)
                ButtonClicked(this, e);

        }

        void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}